#include <iostream>
#include <vector>
#include <map>
using namespace std;

class Solution {
	vector<int> ct;
	vector<int> need;
	bool can(int status, int n, vector<vector<int>> &dp)
	{
		if (status == 0) {
			return true;
		}

		if (n == ct.size()) {
			return false;
		}

		if (dp[status][n] != -1) {
			return dp[status][n];
		}

		for (int j = status; j > 0; j = (j - 1) & status) {
			if (ct[n] >= need[j] && can(status ^ j, n + 1, dp)) {
				return dp[status][n] = true;
			}
		}
		return dp[status][n] = can(status, n + 1, dp);
	}

    public:
	bool canDistribute(vector<int> &nums, vector<int> &quantity)
	{
		map<int, int> cnt;
		for (auto v : nums) {
			cnt[v]++;
		}
		for (auto v : cnt) {
			ct.push_back(v.second);
		}

		int m = quantity.size();
		need.resize(1 << m, 0);

		for (int i = 0; i < m; i++) {
			int v = quantity[i];
			int h = (1 << i);
			for (int j = 0; j < h; j++) {
				need[h | j] = need[j] + v;
			}
		}
		vector<vector<int>> dp(1 << m, vector<int>(ct.size(), -1));
		return can((1 << m) - 1, 0, dp);
	}
};

int main()
{
	vector<int> nums = { 1, 2, 3, 3 }, quantity = { 2 };
	cout << Solution().canDistribute(nums, quantity) << endl;
	return 0;
}